import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useCounterStore = defineStore('cart', () => {
    const carts = ref([])
    function addCart(cartData, goodsid) {
        let username = localStorage.getItem('username')
        let usercarts = carts.value.filter(item => item.username == username)
        let pdCarts = usercarts.find(item => item.goods.goods_id == goodsid)
        if (pdCarts) {
            pdCarts.number++
        }
        else {
            carts.value.push({
                username: username,
                goods: cartData,
                number: 1,
                id: Math.floor(Math.random() * 1000000)
            })
        }   
    }
    function detail(id) {
        carts.value = carts.value.filter((item) => item.id != id)
    }
    return { carts, addCart, detail }
},
    {
        persist: true,
    },)